package si.yun.service;

import org.apache.lucene.util.QueryBuilder;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName ElasticSearchService
 * @Content es
 * @Author 姚龙腾
 * @Date 2022/12/16 9:16
 */
@Service
public class ElasticSearchService {
    @Autowired
    private RestHighLevelClient restHighLevelClient;

    public List<Map<String,Object>> searchPage(
            String index,
            String title,
            String className,
            String content,
            int page,
            int size) throws IOException {
        System.out.println(index);
        System.out.println(title);
        System.out.println(className);
        System.out.println(content);

        List<Map<String,Object>> list = new ArrayList<>();
//        if (page <= 1){
//            page = 1;
//        }
        // 创建搜索请求对象
        SearchRequest searchRequest = new SearchRequest(index);
        // 构建查询的请求体
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //分页
        sourceBuilder.from(0);
        sourceBuilder.size(200);

        //匹配查询
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //必须包含
//        boolQueryBuilder.must(QueryBuilders.matchQuery(keyword, content));
//        sourceBuilder.query(boolQueryBuilder);


        MultiMatchQueryBuilder field = QueryBuilders.multiMatchQuery
                (content, title, className)
                // 设置 name 10倍权重
                .field(className, 10);
        sourceBuilder.query(field);


        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        //高亮
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.field(title);
        highlightBuilder.requireFieldMatch(false);
        highlightBuilder.preTags("<span style='color:red'>");
        highlightBuilder.postTags("</span>");
        sourceBuilder.highlighter(highlightBuilder);

//        TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery(keyword, content);
//        sourceBuilder.query(termQueryBuilder);
//        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

        //执行搜索
        searchRequest.source(sourceBuilder);
        SearchResponse search = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

        //解析结果
        for (SearchHit hit: search.getHits().getHits()) {
            System.out.println(hit.getSourceAsMap());
            //解析高亮的字段
            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
            HighlightField Title = highlightFields.get(title);
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            if (Title != null){
                Text[] fragments = Title.fragments();
                String n_title = "";
                for (Text text : fragments) {
                    n_title += text;
                }
                sourceAsMap.put(title,n_title);
            }
            list.add(sourceAsMap);
        }
        return  list;
    }
}
